package com.zzyk.microCloud.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.zzyk.main.configuration.exception.UserException;
import com.zzyk.main.model.pojo.SysUser;
import com.zzyk.main.model.vo.Message;
import com.zzyk.main.model.vo.WxUser;
import com.zzyk.main.service.IMsgService;
import com.zzyk.main.utils.TencentAppUtils;
import org.apache.http.HttpException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/api/sys")
public class ApiSysController extends ApiBaseController {
    @Autowired
    private IMsgService msgService;

    @RequestMapping("register")
    public Message register(SysUser sysUser,
                            @RequestParam(value = "captcha") String captcha,
                            @RequestParam("jsCode") String jsCode) throws UserException, IOException, HttpException {
        if (sysUser == null) {
            throw new UserException("请输入注册信息");
        }
        String code = msgService.get(sysUser.getUsername());
        if (code == null || !code.equalsIgnoreCase(captcha)) {
            throw new UserException("验证码错误");
        }
        SysUser user = userService.getUser(sysUser.getUsername());
        if (user != null) {
            throw new UserException("用户已注册");
        }
        if (userService.addUser(sysUser) > 0) {
            String result = TencentAppUtils.code2Session(jsCode);
            Map map = (Map) JSONObject.parse(result);
            String openId = (String) map.get("openid");

            WxUser wxUser = new WxUser();
            wxUser.setUserId(sysUser.getId());
            wxUser.setWxOpenId(openId);
            userService.addWxUser(wxUser);
            return Message.success("注册成功").addData("data", userService.getUserWithRole(wxUser.getUserId()));
        } else {
            throw new UserException("注册失败");
        }
    }

    @RequestMapping("unregister")
    public Message unregister() throws UserException {
        WxUser user = getUser();
        if (userService.delWxUser(user.getId()) > 0) {
            return Message.success();
        } else {
            throw new UserException("用户解绑失败");
        }
    }

    @RequestMapping("info")
    public Message info() throws UserException {
        WxUser user = getUser();
        return Message.success().addData("data", userService.getUserWithRole(user.getUserId()));
    }

    @RequestMapping("changePassword")
    public Message changePassword(@RequestParam("oldPassword") String oldPassword,
                                  @RequestParam("newPassword") String newPassword) throws UserException {
        WxUser user = getUser();
        SysUser sysUser = userService.getUser(user.getUserId());
        userService.checkPassword(sysUser.getUsername(), oldPassword);
        sysUser.setPassword(newPassword);
        userService.updateUser(sysUser);
        return Message.success();
    }

    @RequestMapping("exists")
    public boolean testPhone(@RequestParam("username") String username) {
        return userService.getUser(username) != null;
    }

    @RequestMapping("changePhone")
    public Message changePhone(@RequestParam("username") String username,
                               @RequestParam("captcha") String captcha) throws UserException {
        if (userService.getUser(username) != null) {
            throw new UserException("手机号已被使用");
        }
        String code = msgService.get(username);
        if (code == null || !code.equalsIgnoreCase(captcha)) {
            throw new UserException("验证码错误");
        }
        WxUser user = getUser();
        SysUser sysUser = userService.getUser(user.getUserId());
        sysUser.setUsername(username);
        if (userService.updateUser(sysUser) > 0) {
            return Message.success("手机号修改成功");
        } else {
            return Message.failed();
        }
    }

    @RequestMapping("forgetPassword")
    public Message forgetPassword(@RequestParam("username") String username,
                                  @RequestParam("captcha") String captcha,
                                  @RequestParam("password") String password) throws UserException {
        String code = msgService.get(username);
        if (code == null || !code.equalsIgnoreCase(captcha)) {
            throw new UserException("验证码错误");
        }
        SysUser sysUser = userService.getUser(username);
        if (sysUser == null) {
            throw new UserException("用户不存在");
        }
        sysUser.setPassword(password);
        if (userService.updateUser(sysUser) > 0) {
            return Message.success();
        } else {
            return Message.failed();
        }
    }
}
